package com.smalls.redshoes.ui.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.widget.ImageView;

import com.smalls.redshoes.R;

public class RoundImageView extends ImageView {
    private int angle;
    private boolean mIsDrawShape = true;
    private RadiusRect mRadiusRect;
    private int radis;

    public class RadiusRect {
        public float bottomLeftRadius;
        public float bottomRightRadius;
        public float topLeftRadius;
        public float topRightRadius;

        public RadiusRect(float tlRadius, float trRaius, float blRadius, float brRaius) {
            this.topLeftRadius = tlRadius;
            this.topRightRadius = trRaius;
            this.bottomLeftRadius = blRadius;
            this.bottomRightRadius = brRaius;
        }

        public void set(float tlRadius, float trRaius, float blRadius, float brRaius) {
            this.topLeftRadius = tlRadius;
            this.topRightRadius = trRaius;
            this.bottomLeftRadius = blRadius;
            this.bottomRightRadius = brRaius;
        }

        public RadiusRect get() {
            return new RadiusRect(this.topLeftRadius, this.topRightRadius, this.bottomLeftRadius, this.bottomRightRadius);
        }
    }

    public RoundImageView(Context context) {
        super(context);
    }

    public RoundImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs);
    }

    public RoundImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(attrs);
    }

    private void init(AttributeSet attrs) {
        if (attrs != null) {
            TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.RoundImageView);
            this.radis = (int) a.getDimension(R.styleable.RoundImageView_image_radis, 10.0f);
            this.angle = (int) a.getDimension(R.styleable.RoundImageView_image_angle, 90.0f);
            this.mRadiusRect = new RadiusRect((float) this.radis, (float) this.radis, (float) this.radis, (float) this.radis);
            a.recycle();
        }
    }

    public void draw(Canvas canvas) {
        if (this.mIsDrawShape) {
            canvas.setDrawFilter(new PaintFlagsDrawFilter(0, 3));
            canvas.clipPath(getShapePath());
        }
        super.draw(canvas);
    }

    public Path getShapePath() {
        return getRoundPath();
    }

    private Path getRoundPath() {
        int width = getWidth();
        int height = getHeight();
        Path path = new Path();
        path.moveTo(this.mRadiusRect.topLeftRadius, 0.0f);
        path.arcTo(new RectF(0.0f, 0.0f, this.mRadiusRect.topLeftRadius * 2.0f, this.mRadiusRect.topLeftRadius * 2.0f), (float) (-this.angle), (float) (-this.angle));
        path.lineTo(this.mRadiusRect.topLeftRadius, 0.0f);
        path.arcTo(new RectF(((float) width) - (this.mRadiusRect.topRightRadius * 2.0f), 0.0f, (float) width, this.mRadiusRect.topRightRadius * 2.0f), (float) (-this.angle), (float) this.angle);
        path.arcTo(new RectF(((float) width) - (this.mRadiusRect.bottomRightRadius * 2.0f), ((float) height) - (this.mRadiusRect.bottomRightRadius * 2.0f), (float) width, (float) height), 0.0f, (float) this.angle);
        path.arcTo(new RectF(0.0f, ((float) height) - (this.mRadiusRect.bottomLeftRadius * 2.0f), this.mRadiusRect.bottomLeftRadius * 2.0f, (float) height), (float) this.angle, (float) this.angle);
        path.lineTo(0.0f, this.mRadiusRect.topLeftRadius);
        path.close();
        return path;
    }
}
